package treeset04;

import treeset.Person;

import java.util.Comparator;
import java.util.TreeSet;

public class Demo {
    public static void main(String[] args) {
        TreeSet<Product> productTreeSet = new TreeSet<>();

        productTreeSet.add(new Product("电视", 3000.0));
        productTreeSet.add(new Product("手机", 5000.0));
        productTreeSet.add(new Product("电脑", 7000.0));
        productTreeSet.add(new Product("冰箱", 2000.0));
        productTreeSet.add(new Product("洗衣机", 1500.0));

        System.out.println("按价格升序排列:");
        for (Product p : productTreeSet) {
            System.out.println(p);
        }

        TreeSet<Product> productTreeSet1 = new TreeSet<>(new Comparator<Product>() {
            @Override
            public int compare(Product o1, Product o2) {
                return (int) (o2.getPrice()-o1.getPrice());
            }
        });

        productTreeSet1.addAll(productTreeSet);

        System.out.println("按价格降序排列:");
        for(Product p:productTreeSet1){
            System.out.println(p);
        }

    }
}
